package com.mmc.delay.container;

import com.mmc.delay.bean.Job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.mmc.delay.constants.RedisKeyConstants.REDIS_POOL_KEY;

public class JobPool implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(JobPool.class);

    private RedisTemplate redisTemplate;

    public JobPool(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private BoundHashOperations getPool () {
        return this.redisTemplate.boundHashOps(REDIS_POOL_KEY);
    }

    public void addJob(Job job) {
        getPool().put(job.getJobId(),job);
    }


    public Job getJob(String jobId) {
        Object o = getPool().get(jobId);
        if(o instanceof Job){
            return (Job)o;
        }
        return null;
    }

    public List<Job> mget(Collection<String> jobIds){
        List<Job> list = getPool().multiGet(jobIds);
        return list;
    }


    public void removeJob (String jobId) {
        getPool().delete(jobId);
    }


    public Map entries(){
        Map entries = getPool().entries();
        return entries;
    }


}
